package com.nti56.springboot.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nti56.springboot.mapper.tmsmapper.MachProductMapper;
import com.nti56.springboot.model.MachInfo;
import com.nti56.springboot.model.MachProduct;
import com.nti56.springboot.model.ProdMstr;
import com.nti56.springboot.utils.Constant;
import com.nti56.springboot.utils.ParamUtil;
import com.nti56.springboot.utils.ParamUtil.Param;

@Service
public class MachProductService {
	
	private static final Logger logger = LoggerFactory.getLogger(MachProductService.class);

	
	@Autowired
	private MachProductMapper machProductMapper;
	
	@Autowired
    private ProdMstrService prodMstrService;
	
	@Autowired
    private MachInfoService machInfoService;
	
	public MachProduct getById(String mpId) {
		return this.machProductMapper.getById(mpId);
	}
	
	public List<MachProduct> findAll(Map params) {
		return this.machProductMapper.findAll(params);
	}
	
	public List<MachProduct> getRealTimeProductList(List<MachInfo> machInfoList,String tableName,String proDate,String shift){
		//实时产量信息
		Param map = ParamUtil.setParam("tableName", tableName).setParam("proDate", proDate);
		List<MachProduct> realTimeProList = this.findAll(map);
		//目标产量信息
		Param param = ParamUtil.setParam("proDate", proDate).setParam("shift", shift);
		List<ProdMstr> targetProList = this.getTargetProductList(param);
		//最终返回对象
		List<MachProduct> mpList = new ArrayList<MachProduct>();
		for(MachInfo m : machInfoList) {
			MachProduct mp = new MachProduct();
			mp.setMachNum(m.getMachNum());
			mpList.add(mp);
		}
		for(MachProduct mpd : mpList) {
			for(ProdMstr pms : targetProList) {
				if(mpd.getMachNum().equals(pms.getMachNum())) {
					mpd.setShift(pms.getShift());
					mpd.setTargetQty(pms.getTargetQty());
					mpd.setItemNo(pms.getItemNo());
					break;
				}
			}
		}
		for(MachProduct mpd : mpList) {
			for(MachProduct mpdReal : realTimeProList) {
				if(mpdReal.getMachNum().equals(mpd.getMachNum())) {
					mpd.setProCount(mpdReal.getProCount());
					break;
				}
			}
		}
		return mpList;
	}
	
	/**
	 * 目标产量
	 * @param params
	 * @return
	 */
	public List<ProdMstr> getTargetProductList(Map params){
		return prodMstrService.findAll(params);
	}
}
